package com.baymax.test.scene.demo

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.HorizontalScrollView
import android.widget.TextView
import androidx.core.view.children
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.drawable.DrawableUtils
import com.baymax.test.scene.internal.GroupedLinearLayout
import com.baymax.test.scene.newSceneFlexContainer
import com.google.android.flexbox.FlexboxLayout
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

class GroupScrollScene : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer {
            val scene = this@GroupScrollScene
            val horScrollCase = HorScrollCase(scene)
            horScrollCase.plug2View(this)

            addButton("range") {
                horScrollCase.range()
            }

        }
    }

    class HorScrollCase(scene: BaseScene) : CoroutineScope by scene, View.OnFocusChangeListener {

        private val scrollView = HorizontalScrollView(scene.requireSceneContext())
        private val linear = GroupedLinearLayout(scene.requireSceneContext()).apply {
            scrollView.addView(this)
            println("linear $scrollView")
        }
        private val data = ('A'..'Z').map { c ->
            TextView(scrollView.context).apply {
                text = (c.code - 'A'.code).toString()
                textSize = 32f
                isFocusableInTouchMode = true
                isFocusable = true
                setPadding(32, 0, 32, 0)
                isClickable = true
                linear.addView(this, MarginLayoutParams(-1, -1).apply {
                    this.leftMargin = 10
                    this.rightMargin = 10
                })
                background = DrawableUtils.createStateDrawable(
                    GradientDrawable().apply {
                        setStroke(1, Color.RED)
                        setColor(Color.YELLOW)
                        cornerRadius = 16f
                    },
                    GradientDrawable().apply {
                        setStroke(1, Color.GREEN)
                        cornerRadius = 16f
                    })
            }
        }

        val r = Random(System.currentTimeMillis())

        init {
            println("init $scrollView")
        }

        fun plug2View(flexboxLayout: FlexboxLayout) {
            flexboxLayout.apply {
                clipChildren = true
                clipToPadding = true
            }
            flexboxLayout.addView(scrollView, 800, 100)
        }

        var index = 0
        fun range() {
            val z = 'Z'.code - 'A'.code
            val size = r.nextInt(1, 4)



            linear.children.forEach {
                it.onFocusChangeListener = this
            }
            var e = 0
            linear.range =  (0 ..size).map {
                val l = e
                e =  z /size * (it + 1)
                GroupedLinearLayout.RangeValue(linear ,genRange(l ,e ))
            }.toSet()
        }

        fun genRange(start: Int, end: Int) :IntRange {
            val s = r.nextInt(end - 2).coerceAtLeast(start)
            val e = r.nextInt(s, end)
            return s..e
        }

        override fun onFocusChange(v: View, hasFocus: Boolean) {
            val index = linear.indexOfChild(v)
            if (hasFocus) {

            }


        }


    }
}